﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using CameraParentClassLibrary;//引用父类相机库的命名空间

namespace Basler
{
    public class Basler : CameraParent
    {
        private int _delayTime;
        /// <summary>
        /// 延时
        /// </summary>
        public override int DelayTime
        {
            get => _delayTime;
            set => _delayTime = value;
        }
        /// <summary>
        /// 线程
        /// </summary>
        Thread thread = null;
        //手动同步事件
        ManualResetEvent manualResetEvent = new ManualResetEvent(false);
        public Basler()
        {
            BrandName = "巴斯勒";
        }

        public override void OpenCamera()
        {
            base.OpenCamera();
            SetLabelText?.Invoke("打开相机");
            thread = new Thread(DoWork);
            //设置为后台线程。然后启动
            thread.IsBackground = true;
            thread.Start();

        }

        public void DoWork()
        {
            while (true)
            {
                manualResetEvent.WaitOne();//阻塞当前所在线程                
                SetLabelText?.Invoke(DateTime.Now.ToString());
                Thread.Sleep(DelayTime);
                //判断相机的状态，如果是采集一张就重新阻塞
                if (CameraWorkState == CamerWork.采集一张)
                {
                    CameraWorkState = CamerWork.None;
                    manualResetEvent.Reset();
                }
            }
        }
        public override void CloseCamera()
        {
            base.CloseCamera();
            SetLabelText?.Invoke("关闭相机");
            thread.Abort();
        }
        public override void CollectOne()
        {
            base.CollectOne();
            manualResetEvent.Set();
        }
        public override void CollectOneContinuous()
        {
            base.CollectOneContinuous();
            manualResetEvent.Set();
        }

    }
}
